-- drop function if exists sm_sc.fv_d_transpose_3d_dloss_dindepdt(anyarray, int[2]);
create or replace function sm_sc.fv_d_transpose_3d_dloss_dindepdt
(
  i_dloss_ddepdt_3d      anyarray      ,
  i_dims                 int[2]        -- array[1, 2] === array[2, 1], array[1, 3] === array[3, 1] or array[2, 3] === array[3, 2]
)
returns anyarray
as
$$
-- declare

begin
  if array_ndims(i_dloss_ddepdt_3d) <> 3
  then 
    raise exception 'ndims of i_dloss_ddepdt_3d should be 3.';
  end if;
  
  if i_dims[1] not in (1, 2, 3) 
    or i_dims[2] not in (1,2,3)
    or i_dims[1] = i_dims[2]
  then 
    raise exception 'illegal i_dims';
  end if;
 
  return sm_sc.fv_opr_transpose_3d(i_dloss_ddepdt_3d, i_dims);
end
$$
language plpgsql stable
parallel safe
cost 100;
-- -- set search_path to sm_sc;
-- select sm_sc.fv_d_transpose_3d_dloss_dindepdt
--   (
--     array
--     [
--       [
--         [1, 2, 3, 4],
--         [5, 6, 7, 8],
--         [9, 10, 11, 12]
--       ],
--       [
--         [21, 22, 23, 24],
--         [25, 26, 27, 28],
--         [29, 30, 31, 32]
--       ]
--     ]
--     , array[1, 2]
--   );
-- select sm_sc.fv_d_transpose_3d_dloss_dindepdt
--   (
--     array
--     [
--       [
--         [1, 2, 3, 4],
--         [5, 6, 7, 8],
--         [9, 10, 11, 12]
--       ],
--       [
--         [21, 22, 23, 24],
--         [25, 26, 27, 28],
--         [29, 30, 31, 32]
--       ]
--     ]
--     , array[1, 3]
--   );
-- select sm_sc.fv_d_transpose_3d_dloss_dindepdt
--   (
--     array
--     [
--       [
--         [1, 2, 3, 4],
--         [5, 6, 7, 8],
--         [9, 10, 11, 12]
--       ],
--       [
--         [21, 22, 23, 24],
--         [25, 26, 27, 28],
--         [29, 30, 31, 32]
--       ]
--     ]
--     , array[2, 3]
--   );